<?php
declare(strict_types=1);

namespace App\AdminApi\Modular\User\Controller;

use App\AdminApi\BasicsController;
use App\Common\Extend\Guard\Auth;
use App\Common\Modular\System\Serve\ConfigureServe;
use Core\HyperfUnit\Response;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Http\Message\ResponseInterface;

#[
    Auth(guard: self::GUARD, label: ['用户管理', '用户管理'], app: self::APP),
    Controller(prefix: "/admin-api/user", server: self::SERVER)
]
class SettingsController extends BasicsController
{
    #[
        GetMapping('settings'),
        Auth(label: ['会员设置'], sign: 'user.settings', view: '/user/settings/index')
    ]
    public function getSettings(ConfigureServe $serve): ResponseInterface
    {
        return Response::Api()->success(
            data: $serve->getForm('user', $this->request->getPathInfo())
        );
    }

    #[
        PostMapping('settings', ['POST', 'GET']),
        Auth(label: ['律师设置'], relyon: ['user.settings'])
    ]
    public function setSettings(ConfigureServe $serve): ResponseInterface
    {
        $post = $this->request->post();
        if ($serve->writeConfiguration('user', $post)) {
            cache()->clear();
            return Response::Api()->success("设置成功");
        }
        return Response::Api()->fail("编辑失败");
    }
}